/*
Storage (save/restore) of properties for 
- page color 
- location enablement
*/

var localStorageAvailable = 0; // Flag if we can store in localstorage

var SETTINGS_enableLocation = 0;
var SETTINGS_meCenter = 0; // Center me setting
var SETTINGS_documentBackgroundColor = "";
var SETTINGS_documentPageColor = "";
var SETTINGS_webOrMobile = "w"; // Not in use right now...
var SETTINGS_parkIconFilter = 1; // Number of car park spaces available for a park location to be shown on the map
var SETTINGS_parkIconFilterFlag = 0; // 1 if user has chosen to filter icons

var defaultWebOrMobile = "w";
var documentDefaultBackgroundColor = "a";  // Default theme color a=black, b=blue
var documentDefaultPageColor = "#ffffff";
var defaultParkIconFilter = 10;
var defaultParkIconFilterFlag = 0;

var storageKey = "parkering-hist-";

//----------------------------------------------
// Check capabilities and restore settings
//----------------------------------------------
function initPropertyStore() {
    if( getInternetExplorerVersion() > -1) {
        localStorageAvailable = 0; // No support
    } else if (Modernizr.localstorage) {
        localStorageAvailable = 1;       
    }
	if (localStorageAvailable == 1) {
		// Get defaults
		SETTINGS_enableLocation = localStorage.getItem(storageKey+"enableLocation");
		if(SETTINGS_enableLocation==null) {
			SETTINGS_enableLocation = 0;
		} 
		SETTINGS_enableLocation = parseInt(SETTINGS_enableLocation);
		
		 SETTINGS_meCenter = localStorage.getItem(storageKey+"centerMe");
		if( SETTINGS_meCenter==null) {
			 SETTINGS_meCenter = 0;
		} 
		 SETTINGS_meCenter = parseInt(SETTINGS_meCenter);
		
		console.log("LOAD : "  + SETTINGS_enableLocation);
		
		SETTINGS_documentBackgroundColor = localStorage.getItem(storageKey+"documentBackgroundColor");
		if(SETTINGS_documentBackgroundColor==null) {
			SETTINGS_documentBackgroundColor = documentDefaultBackgroundColor;
		}
		
		SETTINGS_documentPageColor = localStorage.getItem(storageKey+"documentPageColor");
		if(SETTINGS_documentPageColor==null) {
			SETTINGS_documentPageColor = documentDefaultPageColor;
		}
	
		SETTINGS_webOrMobile  = localStorage.getItem(storageKey+"webOrMobile");
		if(SETTINGS_webOrMobile==null) {
			SETTINGS_webOrMobile = defaultWebOrMobile;
		}

		SETTINGS_parkIconFilter  = localStorage.getItem(storageKey+"parkIconFilter");
		if(SETTINGS_parkIconFilter==null) {
			SETTINGS_parkIconFilter = defaultParkIconFilter;
		}
		SETTINGS_parkIconFilter  = parseInt(SETTINGS_parkIconFilter);
		
		SETTINGS_parkIconFilterFlag  = localStorage.getItem(storageKey+"parkIconFilterFlag");
		if(SETTINGS_parkIconFilterFlag==null) {
			SETTINGS_parkIconFilterFlag = defaultParkIconFilterFlag;
		}		
		SETTINGS_parkIconFilterFlag  = parseInt(SETTINGS_parkIconFilterFlag);
		
		// Set the properties (visuals usually)
		setSelectedProperties();
	} 
}

//----------------------------------------------
// Set location enablement on/off
function setLocationEnabled(value) {
	SETTINGS_enableLocation = value;
}

//----------------------------------------------
// Save settings if available
function saveProperties() {
	if(localStorageAvailable == 1) {
		localStorage.setItem(storageKey+"enableLocation", SETTINGS_enableLocation);
		localStorage.setItem(storageKey+"centerMe", SETTINGS_meCenter);
		localStorage.setItem(storageKey+"documentBackgroundColor", SETTINGS_documentBackgroundColor);
		localStorage.setItem(storageKey+"documentPageColor", SETTINGS_documentPageColor);
		localStorage.setItem(storageKey+"webOrMobile", SETTINGS_webOrMobile);
		localStorage.setItem(storageKey+"parkIconFilter", SETTINGS_parkIconFilter);
		localStorage.setItem(storageKey+"parkIconFilterFlag", SETTINGS_parkIconFilterFlag);
	}
}

//----------------------------------------------
// Set selected properties will update the system based on the current settings.
function setSelectedProperties() {
	//useDocumentColor(SETTINGS_documentBackgroundColor);
	//setCheckboxFilter(SETTINGS_parkIconFilterFlag);
	// Set check..
	console.log("SET : "  + SETTINGS_enableLocation);
	$("#chkShowPosisition").attr("checked",(SETTINGS_enableLocation==1 ? true : false)).checkboxradio("refresh");
	$("#chkMegISentrum").attr("checked",(SETTINGS_meCenter==1 ? true : false)).checkboxradio("refresh");
	$(".ui-page").css("background",SETTINGS_documentPageColor);
	$('#parkIconFilterValue').val(SETTINGS_parkIconFilter+"");
	$("#filterFlagCheckBox").attr("checked",(SETTINGS_parkIconFilterFlag==1 ? true : false)).checkboxradio("refresh");
}

//----------------------------------------------
// Theming
function setDocumentColor(colValue) {
	SETTINGS_documentBackgroundColor=colValue;	
	//useDocumentColor(colValue);
	saveProperties();
}

